<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const rule = ref([
    {
        "rulesId": "",
        "year": "",
        "academicScoreWeight": "",
        "politicalScoreWeight":"",
        "researchScoreWeight":"",
        "socialScoreWeight": "",
        "awardPeople":""
    }
])

//声明一个异步的函数 页面加载完毕之后 调用articleCategoryListService方法来获取所有文章的分类列表 调用这个函数时同步等待他返回的结果
import {rulesListService,rulesAddService,rulesUpdateService,rulesDeleteService} from '@/api/rules.js'
const rulesList = async()=>{
    let result = await rulesListService();
    rule.value=result.data//result.data列表数据 赋值给student
}
rulesList();//调用


//控制添加分类弹窗 默认不显示
const dialogVisible = ref(false)

//添加分类数据模型
const ruleModel = ref({
    rulesId:'',
    year: null,
    academicScoreWeight: null,
    politicalScoreWeight:null,
    researchScoreWeight:null,
    socialScoreWeight:null,
    awardPeople:""
})
//添加分类表单校验
const rules = {
    rulesId: [
        { required: true, message: '请输入规则号', trigger: 'blur' },
    ],
    year: [
        { required: true, message: '请输入规则年份', trigger: 'blur' },
    ],
    academicScoreWeight: [
        { required: true, message: '请输入课程权重', trigger: 'blur' },
    ],
    politicalScoreWeight: [
        { required: true, message: '请输入思政权重', trigger: 'blur' },
    ],
    researchScoreWeight: [
        { required: true, message: '请输入科研权重', trigger: 'blur' },
    ],
    socialScoreWeight: [
        { required: true, message: '请输入社会权重', trigger: 'blur' },
    ],
    awardPeople: [
        { required: true, message: '请输入获奖人数', trigger: 'blur' },
    ]
}

//调用接口 添加表单
import {ElMessage} from 'element-plus'
const addRules = async()=>{
    //调用接口 同步等待异步调用的结果
   let result =  await rulesAddService(ruleModel.value)
   ElMessage.success(result.msg? result.msg:'添加成功')// msg有东西就输出 没东西就添加成功

   //调用获取所有文章文类的函数
   rulesList();
   //消失当前弹窗
   dialogVisible.value=false
}

//定义变量 控制标题的展示
const title = ref('')

//展示编辑弹窗 数据回写 把原数据展示出来
const showDialog = (row)=>{
    console.log('点击编辑的行数据 row：', row);
    dialogVisible.value=true;title.value = '编辑规则'
    //数据拷贝
    ruleModel.value.rulesId = row.rulesId
    ruleModel.value.year = row.year;
    ruleModel.value.academicScoreWeight = row.academicScoreWeight;
    ruleModel.value.politicalScoreWeight = row.politicalScoreWeight;
    ruleModel.value.researchScoreWeight = row.researchScoreWeight;
    ruleModel.value.socialScoreWeight = row.socialScoreWeight;
    ruleModel.value.awardPeople = row.awardPeople;
}

//更改学生
const updateRules = async() =>{
    //调用接口
    console.log('准备提交修改的 studentModel 值：', ruleModel.value); 
    let result = await rulesUpdateService(ruleModel.value);
    ElMessage.success(result.msg?result.msg:'修改成功')

    //调用获取所有学生函数
    rulesList();
    
    dialogVisible.value = false;
}

//删除学生
import {ElMessageBox} from 'element-plus'
const deleteRules = async(row)=>{
    //提示用户 展示确认框

        ElMessageBox.confirm(
    '确认删除该规则吗?',
    'Warning',
    {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning',
    }
    )
    .then(async() => {
        //调用接口
        let result = await rulesDeleteService(row.rulesId)
    ElMessage({
        type:'success',
        message: 'Delete completed',
    })
    //刷新列表
    rulesList();
    })
    .catch(() => {
    ElMessage({
        type: 'info',
        message: 'Delete canceled',
    })
    })
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>规则管理</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible=true;title = '添加规则'">添加规则</el-button>
                </div>
            </div>
        </template>
        <el-table :data="rule" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="规则号" prop="rulesId"></el-table-column>
            <el-table-column label="年份" prop="year"></el-table-column>
            <el-table-column label="课程权重" prop="academicScoreWeight"></el-table-column>
            <el-table-column label="思政权重" prop="politicalScoreWeight"></el-table-column>
            <el-table-column label="科研权重" prop="researchScoreWeight"></el-table-column>
            <el-table-column label="社会权重" prop="socialScoreWeight"></el-table-column>
            <el-table-column label="获奖人数" prop="awardPeople"></el-table-column>
            <el-table-column label="操作" width="100">
                <!--表示这一行的数据-->
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteRules(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加分类弹窗 -->
<el-dialog v-model="dialogVisible" :title="title" width="30%">
    <el-form :model="ruleModel" :rules="rules" label-width="100px" style="padding-right: 30px">
        <el-form-item label="规则号" prop="rulesId">
            <el-input v-model="ruleModel.rulesId" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="规则年份" prop="year">
            <el-input v-model="ruleModel.year" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="课程权重" prop="academicScoreWeight">
            <el-input v-model="ruleModel.academicScoreWeight" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="思政权重" prop="politicalScoreWeight">
            <el-input v-model="ruleModel.politicalScoreWeight" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        <el-form-item label="科研权重" prop="researchScoreWeight">
            <el-input v-model="ruleModel.researchScoreWeight" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        <el-form-item label="社会权重" prop="socialScoreWeight">
            <el-input v-model="ruleModel.socialScoreWeight" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        <el-form-item label="获奖人数" prop="awardPeople">
            <el-input v-model="ruleModel.awardPeople" minlength="1" maxlength="15"></el-input>
        </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="title=='添加规则' ?addRules():updateRules()"> 确认 </el-button>
        </span>
    </template>
</el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>